package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.proj.entity.SystemColumnEntity;
import com.ts.proj.repository.SystemColumnRepository;
import com.ts.proj.service.SystemColumnService;
import com.ts.util.PageData;
import com.ts.util.StringsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SystemColumnServiceImpl extends CommonServiceImpl<SystemColumnEntity> implements SystemColumnService {

    @Autowired
    private SystemColumnRepository systemColumnRepository;

    @Override
    public List<SystemColumnEntity> findAll(PageData pageData, Integer start, Integer length) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        //使用 CriteriaBuilder方式来做过滤
        Specification<SystemColumnEntity> sp = this.getSpecification(pageData,SystemColumnEntity.class);
        List<SystemColumnEntity> all = systemColumnRepository.findAll(sp,pageable).getContent();
        return all;
    }

    @Override
    public long count(PageData pageData) {
        return systemColumnRepository.count(this.getSpecification(pageData,SystemColumnEntity.class));
    }

    @Override
    public List<SystemColumnEntity> saveAll(List<SystemColumnEntity> columnEntities) {
        return systemColumnRepository.saveAll(columnEntities);
    }

    @Override
    public List<SystemColumnEntity> findAllById(Integer[] ids) {
        List<SystemColumnEntity> list = systemColumnRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemColumnEntity equipmentEntity = new SystemColumnEntity();
            list.add(equipmentEntity);
        }
        return list;
    }

    @Override
    public boolean deleteByIds(Iterable<SystemColumnEntity> iterable) {
        systemColumnRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public boolean deleteAll() {
        return false;
    }
    /**
     *
     * @return
     */
    @Override
    public List<String> findAllTables() {
        List<String> list = systemColumnRepository.findAllTables();
        List<String> resultList = new ArrayList<>();
        for(String str : list){
            resultList.add(StringsUtil.upperCase(str.replace("ta_",""))+"Entity");
        }
        return resultList;
    }

}
